package pkg

import (
	"context"
	"fmt"

	"github.com/go-redis/redis/v8"
)

var DBRedis *redis.Client

func init() {
	// Redis 连接信息
	address := "r-2ze7egv1j6dfikncihpd.redis.rds.aliyuncs.com:6379"
	username := "r-2ze7egv1j6dfikncih"
	password := "Aceshi2024@" // 如果没有设置密码则为空字符串
	db := 1                   // 默认数据库编号

	// 建立连接
	client := redis.NewClient(&redis.Options{
		Addr:     address,
		Username: username,
		Password: password,
		DB:       db,
	})

	// 测试连接是否成功
	pong, err := client.Ping(context.Background()).Result()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Redis 连接成功：", pong)
	DBRedis = client
	// 记得关闭连接，释放资源
	// defer client.Close()
}
